import {
  fetchRegionList,
} from '@/services/system/Common';
import {arr2tree} from '@/utils/utils';


export default {
  namespace: 'SystemCommon',

  state: {
    regionTree: [],
    regionList: [],
  },

  effects: {

    * fetchRegionList({payload}, {call, put}) {
      const response = yield call(fetchRegionList, payload);
      if (response.code === 0) {
        yield put({
          type: 'chgRegionList',
          payload: response.data,
        });
        yield put({
          type: 'chgRegionTree',
          payload: arr2tree(response.data.map(d => {
            d.label = d.name;
            d.value = d.id;
            d.key = d.id;
            return d;
          })),
        });
      }
    },

  },

  reducers: {
    chgRegionTree(state, action) {
      return {
        ...state,
        regionTree: action.payload,
      };
    },
    chgRegionList(state, action) {
      return {
        ...state,
        regionList: action.payload,
      };
    },

  },

};